-
Notifications
You must be signed in to change notification settings - Fork 492
Update action types definitions in order to work with typescript 2.1.4 #88
Conversation
- action types are now instances of anonymous classe - action types properties are readonly - the type in every action class is readonly - the type function now only accepts parameters that extend string
- the classes have static properties describing the action keys - this was done in order to prevent later version of angular-cli complaining the annonymous class wasn't exported
Instead of rewriting the For example export const ActionTypes = {
SEARCH: type<'[Book] Search'>('[Book] Search'),
SEARCH_COMPLETE: type<'[Book] Search Complete'>('[Book] Search Complete'),
LOAD: type<'[Book] Load'>('[Book] Load'),
SELECT: type<'[Book] Select'>('[Book] Select')
}; Edit: |
Well, writing the same thing twice seems a bit counter intuitive. |
Sure, I guess that's about preference. I just wanted to use what's available instead of rewriting to classes. I had to do the same for my project which had a whole bunch of actions. |
WrathOfZombies, ADD_ITEM: type('[WFState] ADD_ITEM'), =TRIM(A1) =FIND("'",B1) =FIND("'",B1,C1+2) =MID(B1,C1,(D1-C1) +1) =SUBSTITUTE(B1,"type","type<"&E1&">",1) Gives ADD_ITEM: type<'[WFState] ADD_ITEM'>('[WFState] ADD_ITEM'), |
We will accept TypeScript 2.1 update merge requests after Angular 4 is released. Here is the conversion we want to make to the actions: Before: import { Action } from '@ngrx/store';
import { type } from '../util';
export const ActionTypes = {
OPEN_SIDENAV: type('[Layout] Open Sidenav'),
CLOSE_SIDENAV: type('[Layout] Close Sidenav')
};
export class OpenSidenavAction implements Action {
type = ActionTypes.OPEN_SIDENAV;
}
export class CloseSidenavAction implements Action {
type = ActionTypes.CLOSE_SIDENAV;
}
export type Actions
= OpenSidenavAction
| CloseSidenavAction; After: import { Action } from '@ngrx/store';
export const OPEN_SIDENAV = '[Layout] Open Sidenav';
export const CLOSE_SIDENAV = '[Layout] Close Sidenav';
export class OpenSidenavAction implements Action {
readonly type = OPEN_SIDENAV;
}
export class CloseSidenavAction implements Action {
readonly type = CLOSE_SIDENAV;
}
export type Actions
= OpenSidenavAction
| CloseSidenavAction; Types will flow correctly and it doesn't require the |
Would this still guarantee action type uniqueness though? Isn't that also what the type method guarantees? |
You can use the type function if you want. It's just that you don't have to
…On Jan 26, 2017 5:42 PM, "John Hamm" ***@***.***> wrote:
Would this still guarantee action type uniqueness though? Isn't that also
what the type method guarantees?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#88 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AJKtHNcmft204bkZJq1BcGT85UykVxQTks5rWL7XgaJpZM4LL3Eu>
.
|
@MikeRyan52 How do the Before/After changes mentioned above affect the usage of the individual |
@MikeRyan52 correct me if I'm wrong, but it looks to me as if we could immediately start using the new action definition style #88 (comment) if we are using typescript 2.1. Is that correct? It seems to work for me, but I may be missing some cases. |
(I've rewritten this comment because I was wrong yesterday.) The following works:
The key is in the
|
Now that we don't need to use
But it does not work. Not even in this form:
And |
@MikeRyan52 The after code
doesn't work for actions with actions having payload and having no payload mixed together. And it doesn't work with different payload types:
In my reducer, it complains that the 'payload' does not exist on type 'Actions'. And even when they all have payload, if the types are different; for example in my case, it will complain slice doesn't exist in any[] | Ticker (when I want to use .slice on my array payload) |
I've just realized the main goal of |
Typescript was upgraded to |
This allows the example-app to be used with typescript 2.1.4.
This means that when not using
readonly
orconst
the variable / property will be a string, instead ofa string literal type (
'[Book] Search'
)This does not affect previous versions of typescript